package com.mvp.shq.washer.ui.shop.mall;

import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.shop.ShopBanner;
import com.mvp.shq.washer.entity.shop.result.ProductResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.T;

import java.util.List;

/**
 * Created by shq1 on 2017/9/29.
 */

public class Presenter extends MallConstract.Presenter {
    @Override
    public void onStart() {
        getRecommentProduct();
        getHotProduct();
    }

    @Override
    void getRecommentProduct() {
        model.getRecommentProduct()
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<ProductResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<ProductResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getRecommentProductS(t.getData().getList());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void getHotProduct() {
        model.getHotProduct()
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<ProductResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<ProductResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getHotProductS(t.getData().getList());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void getBanner() {
        model.getBanner()
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<List<ShopBanner>>(view) {
                    @Override
                    protected void onSuccees(BaseResult<List<ShopBanner>> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getBannerS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }
}
